//
//  DEWPopRingView.m
//  OtherDemo
//
//  Created by kangbinbin on 2018/7/29.
//  Copyright © 2018年 lianjie. All rights reserved.
//

#import "DEWPopRingView.h"
#import "MBProgressHUD+Add.h"
#define ALerView_w DEWScreenWidth - 46

@interface DEWPopRingView()<UITableViewDelegate,UITableViewDataSource>
/** <#type#> */
@property (strong, nonatomic) UIView *AlertView;
/** <#type#> */
@property (strong, nonatomic) UITableView *tableView;

/** 是否选中 */
@property (strong, nonatomic) NSMutableArray *selectArr;


@end

@implementation DEWPopRingView

+ (DEWPopRingView *)showPopRingViewDataArr:(NSMutableArray *)Arr  pushbtnBlock:(pushbtnBlock)pushbtnBlock{
    DEWPopRingView *Pop = [[DEWPopRingView alloc]initWithDataArr:Arr  pushbtnBlock:pushbtnBlock];
    return Pop;
    
}
-(instancetype)initWithDataArr:(NSMutableArray *)DataArr pushbtnBlock:(pushbtnBlock)pushbtnBlock{
    if (self = [super init]) {
        self.dataArr = [DataArr mutableCopy];
        for (NSInteger i = 0 ; i < self.dataArr.count; i ++) {
            [self.selectArr addObject:@"1"];
        }
        _pushbtnBlock = [pushbtnBlock copy];
        [self creatView];
    }
    return self;
}

- (void)creatView{
    [self initUI];
    
    [self show];
    
    
}
-(void)initUI{
    
    self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    self.frame = CGRectMake(0, 0, DEWScreenWidth, DEWScreenHeight);
    
    UIView *AlertView = [[UIView alloc]initWithFrame:CGRectMake(23, 124, DEWScreenWidth - 46 , DEWScreenHeight - 124 * 2)];
    AlertView.backgroundColor = [UIColor whiteColor];
    AlertView.layer.cornerRadius = 10;
    [self addSubview:AlertView];
    self.AlertView = AlertView;
    
    UILabel *titleL = [[UILabel alloc]init];
    titleL.frame = CGRectMake(0, 30, AlertView.width, 30);
    titleL.text = @"请选择你要发布的圈子";
    titleL.textColor = UIColorFromRGB(0x333333);
    titleL.font = [UIFont systemFontOfSize:19.f];
    titleL.textAlignment = NSTextAlignmentCenter;
    [AlertView addSubview:titleL];
  
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, titleL.Wbotoom + 5, AlertView.width , AlertView.height - 60 - 70) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [AlertView addSubview:_tableView];
    if (@available(iOS 11.0, *)){
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.tableView.estimatedRowHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
    }
    
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.size = CGSizeMake(30, 30);
    cancelBtn.centerX = self.centerX;
    cancelBtn.y = AlertView.bottom + 20;
    [cancelBtn setImage:[UIImage imageNamed:@"关闭弹窗"] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    cancelBtn.tag = 51;
    [self addSubview:cancelBtn];
    
    
    UIButton *quedingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [AlertView addSubview:quedingBtn];
    quedingBtn.frame = CGRectMake(25,_tableView.bottom + 5, AlertView.width - 50, 40);
    [quedingBtn setTitle:@"发布" forState:UIControlStateNormal];
    [quedingBtn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
    [quedingBtn setBackgroundColor:UIColorFromRGB(0xFF8900)];
    [quedingBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    quedingBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    quedingBtn.layer.cornerRadius = 4;
    quedingBtn.tag = 50;
    
    
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count;
    
}

-(void)buttonClick:(UIButton *)sender{
    if (sender.tag == 50) {//确定
        NSInteger k = 0;
        for (NSInteger i = 0 ; i < self.selectArr.count; i ++) {
            NSString *str  = self.selectArr[i];
            if ([str isEqualToString:@"0"]) {
                if (self.pushbtnBlock) {
                    self.pushbtnBlock(self.dataArr[i], i);
                }
                k++;
            }
        }
        if (k == 0 ) {
            [MBProgressHUD showError:@"请选择你要发布的圈子" toView:self];
            return;
        }
        
        [self closeViewTime:0.5];
    }else{//取消
        
        [self closeViewTime:0.5];
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellID = [NSString stringWithFormat:@"popRing%ld%ld",(long)indexPath.section,(long)indexPath.row];
    UITableViewCell *cell = nil;
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UILabel *titleL = [[UILabel alloc]init];
    titleL.frame = CGRectMake(20, 0, ALerView_w - 40, 40);
    titleL.text = self.dataArr[indexPath.row];
    titleL.font = [UIFont systemFontOfSize:15];
    NSString *str = self.selectArr[indexPath.row];
    titleL.textAlignment = NSTextAlignmentCenter;
    if ([str isEqualToString:@"1"]) {
        titleL.textColor = Color_99;
        titleL.backgroundColor = UIColorFromRGB(0XF5F5F5);
            }else{
        titleL.textColor = UIColorFromRGB(0xFF8900);
        titleL.backgroundColor = UIColorFromRGB(0XFFF1E0);
    }
    [cell addSubview:titleL];
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    for (NSInteger i = 0 ; i < self.dataArr.count; i ++) {
        NSString *str = self.selectArr[i];
        if (i == indexPath.row) {
            if ([str isEqualToString:@"1"]) {
                [self.selectArr replaceObjectAtIndex:i  withObject:@"0"];
            }
        }else{
            if ([str isEqualToString:@"0"]) {
                [self.selectArr replaceObjectAtIndex:i  withObject:@"1"];
            }
        }
    }
    [self.tableView reloadData];
    
    
}

-(void)show{
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    self.AlertView.alpha = 0;
    [UIView animateWithDuration:0.5 animations:^{
        self.AlertView.alpha = 1;
    }];
    
}
-(void)closeViewTime:(NSTimeInterval)duration{
    [UIView animateWithDuration:duration animations:^{
        self.alpha = 0;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    UITouch *touch = [touches anyObject];
    CGPoint pt = [touch locationInView:self];
    if (!CGRectContainsPoint([self.AlertView frame], pt)) {
        [self closeViewTime:0.5];
    }
    
}
-(NSMutableArray *)selectArr{
    if (_selectArr == nil) {
        _selectArr = [NSMutableArray array];
    }
    return _selectArr;
}

@end
